package com.javasm.crm.service.giftservice.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.javasm.crm.domin.criteria.GiftInfoCriteria;
import com.javasm.crm.domin.entity.gift.BindGiftList;
import com.javasm.crm.domin.entity.gift.GiftBind;
import com.javasm.crm.domin.entity.gift.GiftBindVo;
import com.javasm.crm.domin.entity.gift.GiftColor;
import com.javasm.crm.mapper.giftmapper.GiftBindMapper;
import com.javasm.crm.service.giftservice.GiftBindService;
import com.javasm.crm.utils.page.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author whl
 * @since 2021-07-16
 */
@Service
public class GiftBindServiceImpl extends ServiceImpl<GiftBindMapper, GiftBind> implements GiftBindService {

	@Autowired
	GiftBindMapper giftBindMapper;

	@Override
	public PageResult<GiftBindVo> searchGiftBindByCriteria(GiftInfoCriteria giftInfoCriteria) {
		Integer currentPage = giftInfoCriteria.getCurrentPage();
		Integer pageSize = giftInfoCriteria.getPageSize();

		if (StringUtils.isEmpty(currentPage)) {
			currentPage = 1;
		}
		if (StringUtils.isEmpty(pageSize)) {
			pageSize = 5;
		}
//        PageHelper.startPage(currentPage, pageSize);
		String name = giftInfoCriteria.getGiftName();
		//条件查询
		QueryWrapper<GiftBindVo> exampleQueryWrapper = new QueryWrapper<>();
//        LambdaQueryWrapper<StudentEntity> lambda = exampleQueryWrapper.lambda();
//
////        if (!StringUtils.isEmpty(name)) {
////            lambda.like(StudentEntity::getName, name);
////        }

		exampleQueryWrapper.eq("gb.deleted", 0);

		if (!StringUtils.isEmpty(giftInfoCriteria.getGoodName())) {
			exampleQueryWrapper.like("bind_goods.good_name", giftInfoCriteria.getGoodName());
		}
		if (!StringUtils.isEmpty(giftInfoCriteria.getGiftName())) {
			exampleQueryWrapper.like("gift_goods.gift_name", giftInfoCriteria.getGiftName());
		}

		if (!StringUtils.isEmpty(giftInfoCriteria.getBindCheckid())) {
			exampleQueryWrapper.eq("gb.bind_checkid", giftInfoCriteria.getBindCheckid());
		}


		Page<GiftBindVo> examplePage = new Page<>(currentPage, pageSize);

		List<GiftBindVo> queryall = giftBindMapper.queryall(examplePage, exampleQueryWrapper);

		PageResult<GiftBindVo> studentEntityPageResult = new PageResult<>(examplePage.getTotal(), queryall);

		return new  PageResult<GiftBindVo>(examplePage.getTotal(),queryall);

	}

	@Override
	public int deleteGiftBindById(Serializable id) {
		return giftBindMapper.deleteById(id);
	}

	@Override
	public List<GiftBindVo> getGiftBindListById(GiftInfoCriteria giftInfoCriteria) {

		QueryWrapper<GiftBindVo> exampleQueryWrapper = new QueryWrapper<>();
		if (!StringUtils.isEmpty(giftInfoCriteria.getBindId())) {
			exampleQueryWrapper.eq("gbind.bind_id", giftInfoCriteria.getBindId());
		}
		List<GiftBindVo> queryGiftBindList = giftBindMapper.queryGiftBindListById(exampleQueryWrapper);
		return queryGiftBindList;
	}

	@Override
	public List<GiftBindVo> getGoodBindListById(GiftInfoCriteria giftInfoCriteria) {

		QueryWrapper<GiftBindVo> exampleQueryWrapper = new QueryWrapper<>();
		if (!StringUtils.isEmpty(giftInfoCriteria.getBindId())) {
			exampleQueryWrapper.eq("gbind.bind_id", giftInfoCriteria.getBindId());
		}
		List<GiftBindVo> queryGoodBindList = giftBindMapper.queryGoodBindListById(exampleQueryWrapper);
		return queryGoodBindList;
	}

}
